package dto;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import util.GameFunction;
import config.GameConfig;
import entity.GameAct;

public class GameDto {

	/**
	 * 游戏宽度
	 */
	public static final int GAMEZONE_W = GameConfig.getSystemConfig().getMaxX() + 1;

	/**
	 * 游戏高度
	 */
	public static final int GAMEZONE_H = GameConfig.getSystemConfig().getMaxY() + 1;
	
	/**
	 * 数据库记录
	 */
	private List<Player> dbRecord;
	
	/**
	 * 本地记录
	 */
	private List<Player> diskRecord;
	
	/**
	 * 游戏地图
	 */
	private boolean[][] gameMap;
	
	/**
	 * 下路方块
	 */
	private GameAct gameAct;
	
	/**
	 * 下一个方块
	 */
	private int next;
	
	/**
	 * 当前等级
	 */
	private int nowLevel;
	
	/**
	 * 当前分数
	 */
	private int nowPoint;
	
	/**
	 * 当前消行数
	 */
	private int nowRemoveLine;
	
	/**
	 * 游戏是否是开始状态
	 */
	private boolean start;
	
	/**
	 * 是否显示阴影
	 */
	private boolean showShadow;
	
	/**
	 * 暂停
	 */
	private boolean pause;
	
	/**
	 * 游戏结束
	 */
	private boolean gameOver;
	
	/**
	 * 是否启用作弊功能
	 */
	private boolean cheat;
	
	/**
	 * 线程睡眠时间
	 */
	private long sleepTime;
	
	/**
	 * 构造函数
	 */
	public GameDto(){
		doInit();
	}
	
	//dto初始化
	public void doInit(){
		this.gameMap = new boolean[GAMEZONE_W][GAMEZONE_H];
		this.nowLevel = 0;
		this.nowPoint = 0;
		this.nowRemoveLine = 0;
		this.pause = false;
		this.gameOver = false;
		this.cheat = false;
		this.sleepTime = GameFunction.getSleepTimeByLevel(this.nowLevel);
	}
	
	public List<Player> getDbRecord() {
		return dbRecord;
	}

	public void setDbRecord(List<Player> dbRecord) {
		this.dbRecord = setFillRecord(dbRecord);
	}

	public List<Player> getDiskRecord() {
		return diskRecord;
	}

	public void setDiskRecord(List<Player> diskRecord) {
		this.diskRecord = setFillRecord(diskRecord);
	}
	
	private List<Player> setFillRecord(List<Player> players){
		//TODO 如果进来的是空，那么就创建
		if(players == null){
			players = new ArrayList<Player>();
		}
		//如果记录数小于5，那么就加到5条为止
		while (players.size() < 5) {
			players.add(new Player("No Data", 0));
		}
		Collections.sort(players);
		return players;
	}
	
	public boolean[][] getGameMap() {
		return gameMap;
	}

	public void setGameMap(boolean[][] gameMap) {
		this.gameMap = gameMap;
	}

	public GameAct getGameAct() {
		return gameAct;
	}

	public void setGameAct(GameAct gameAct) {
		this.gameAct = gameAct;
	}

	public int getNext() {
		return next;
	}

	public void setNext(int next) {
		this.next = next;
	}

	public int getNowLevel() {
		return nowLevel;
	}

	public void setNowLevel(int nowLevel) {
		this.nowLevel = nowLevel;
		//计算线程睡眠时间（下落速度）
		this.sleepTime = GameFunction.getSleepTimeByLevel(this.nowLevel);
	}

	public int getNowPoint() {
		return nowPoint;
	}

	public void setNowPoint(int nowPoint) {
		this.nowPoint = nowPoint;
	}

	public int getNowRemoveLine() {
		return nowRemoveLine;
	}

	public void setNowRemoveLine(int nowRemoveLine) {
		this.nowRemoveLine = nowRemoveLine;
	}

	public boolean isStart() {
		return start;
	}

	public void setStart(boolean start) {
		this.start = start;
	}

	public boolean isShowShadow() {
		return showShadow;
	}

	public void changeShowShadow() {
		this.showShadow = ! this.showShadow;
	}

	public boolean isPause() {
		return pause;
	}

	public void changePause() {
		this.pause = ! this.pause;
	}

	public boolean isCheat() {
		return cheat;
	}

	public void setCheat(boolean cheat) {
		this.cheat = cheat;
	}

	public long getSleepTime() {
		return sleepTime;
	}

	public boolean isGameOver() {
		return gameOver;
	}

	public void changeGameOver() {
		this.gameOver = ! this.gameOver;
	}
}
